<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class wms_ctl_admin_stockapp extends desktop_controller{

    var $workground = 'wms_ctl_admin_stockapp';
    var $pagelimit = 10;
    var $stocknotice_model ;
    public function __construct($app)
    {
        parent::__construct($app);
        $this->member_model = $this->app->model('stockapp');
        header("cache-control: no-store, no-cache, must-revalidate");
    }
    //备货通知单首页
   function index()
   {

        $filter=array();
       // $is_super = $this->user->is_super();
       // if($is_super != 1){
       //     $filter['operator'] = $this->user->get_name();
       // }

        $params = array(
            'title' => '备货申请表',
            'actions' => array(
                'new' => array(
                    'label' => '新建',
                    'href' => 'index.php?app=wms&ctl=admin_stockapp&act=add_stockapp',
                        'target' => '_blank',
                ),
                'confirm' => array(
                    'label' => '确认',
                    'submit' => 'index.php?app=wms&ctl=admin_stockapp&act=doConfirm',
                ),
                'undoconfirm' => array(
                    'label' => '取消确认',
                    'submit' => 'index.php?app=wms&ctl=admin_stockapp&act=doundoConfirm',
                ),
                'print' => array(
                    'label' => '打印',
                    'submit' => 'index.php?app=wms&ctl=admin_stockapp&act=doPrint',
                        'target' => '_blank',
                ),
                'delete' => array(
                    'label' => '删除',
                    'submit' => 'index.php?app=wms&ctl=admin_stockapp&act=doDelete',
                ),
            ),
            'base_filter' => $filter,
            //'base_filter' => '',
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag' => false,
            'use_buildin_recycle' => false,
            'use_buildin_export' => true ,
            'use_buildin_import' => false,
            'use_buildin_filter' => true,
            'allow_detail_popup' => true,
            'max_actions' => 8,
            'use_view_tab' => false,

            'orderBy' => 'create_time desc',
        );
        $this->finder('wms_mdl_stockapp', $params);

    }
    //添加备货
    function add_stockapp()
    {

        //add by ldward 2018-09-28 根据店铺查询所有的AX客户
        $shopIds=implode("','", $this->shopIds);
        $sqlcustomer="SELECT distinct AXCustomer,customername 
            from sdb_ome_shop f inner join sdb_wms_delivery e on f.shop_id=e.shop_id
            where e.shop_id in('$shopIds') and f.AXCustomer!='' and e.print_status=0 and e.status=0 and e.is_ship='false' and e.is_form='false' order by f.AXCustomer";
        $customer_data = kernel::database()->select($sqlcustomer);
        foreach ($customer_data as $key => $value) 
        {
            $customers[]= array('AXCustomer' => $value['AXCustomer'],'customername'=>'【'.$value['AXCustomer'].'】'.$value['customername']);   
        }
        $this->pagedata['stockapp']['customers'] = $customers;
        $operator = kernel::single('desktop_user')->get_name();
        $this->pagedata['stockapp']['operator'] = $operator;
        $this->pagedata['stockapp']['stockapp_bn']=$this->autocreatebn();

        $this->singlepage('admin/stockapp/stockapp_add.html');
    }
    function autocreatebn()
    {
        $bn='';
        $y = date("Y");
        $m = date("m");
        $d = date("d");
        $time_begin= mktime(0,0,0,$m,$d,$y);
        $time_end= mktime(23,59,59,$m,$d,$y);
        $delivery_model = $this->app->model('stockapp');
        $filter['create_time|between'] = array($time_begin, $time_end);
        $delivery= $delivery_model->getList('stockapp_bn',$filter,0,1,'create_time desc');
        if($delivery[0]['stockapp_bn']=='')
        {
            $bn ='SA'.date('Ymd',time()).'001';
        }
        else
        {
            $bn=substr($delivery[0]['stockapp_bn'] , 10 , 3);
            $num=intval($bn)+1;
            if($num>9 && $num<=99)
            {
                $bn ='SA'.date('Ymd',time()).'0'.$num;
            }elseif($num<=9)
            {
                $bn ='SA'.date('Ymd',time()).'00'.$num;
            }else
            {
                $bn ='SA'.date('Ymd',time()).$num;
            }
        }
        return $bn;
    }


    /**
     * 基础物料列表弹窗数据获取方法
     *
     * @param Void
     * @return String
     */
    function finddelivery($supplier_id=null)
    {
        $base_filter['print_status'] = '0';
        $base_filter['status'] = '0';
        $base_filter['is_ship'] = 'false';
        $base_filter['deliverytype'] = array('pickup','delivery','auto','batch','single');
        $base_filter['is_form'] = 'false';
        //add by ldward 20180927 备货审批权限配置，根据客户编码查询所有对应的店铺数据
        $AXCustomer=$_GET['AXCustomer'];
        //update by ldward 20180928 根据客户授权，这种过滤方式取消
        if(!$_POST['shop_id'])
        {
            $condition = $this->shopIds;
        }else{
            $condition = $_POST['shop_id'];
        }
        $shopIds=implode("','", $condition);

        $sqlcustomer="SELECT shop_id from sdb_ome_shop where shop_id in('$shopIds') and AXCustomer='$AXCustomer' order by AXCustomer";
        $customer_data = kernel::database()->select($sqlcustomer);
        foreach ($customer_data as $key => $value) 
        {
            $cdshopIds[]=$value['shop_id'];
        } 
        //添加仓库过滤

        //获取操作员管辖仓库
        if ($_GET['btype'] == 2) {
            //导出的时候走这里取第三方仓
            $ownerBranch = array();
            $ownerBranch = kernel::single('ome_branch_type')->getOtherBranchLists();
        } else {
            //所有自建仓
            $ownerBranch = array();
            $ownerBranch = kernel::single('ome_branch_type')->getOwnBranchIds();
        }

        # 获取操作员管辖仓库
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super) {
            $oBranch = app::get('ome')->model('branch');
            $branch_ids = $oBranch->getBranchByUser(true);
            if ($branch_ids) {
                $base_filter['ext_branch_id'] = $_POST['branch_id'] ? $_POST['branch_id'] : $branch_ids;

                $base_filter['ext_branch_id'] = array_intersect($base_filter['ext_branch_id'], $ownerBranch);
            } else {
                $base_filter['ext_branch_id'] = 'false';
            }
        } else {
            $base_filter['ext_branch_id'] = $ownerBranch;
        }
        $base_filter['shop_id']=$cdshopIds;
        $params = array(
                'title'=>'发货单',
                'use_buildin_new_dialog' => false,
                'use_buildin_set_tag'=>false,
                'use_buildin_recycle'=>false,
                'use_buildin_export'=>false,
                'use_buildin_import'=>false,
                'use_buildin_filter'=>true,
                'base_filter' => $base_filter,
        );
        $this->finder('wms_mdl_delivery', $params);
    }

    function getdeliverys()
    {
        //$basicMaterialSelect    = kernel::single('delivery_select');#基础物料 ExBOY
        //$basicMaterialBarcode    = kernel::single('material_basic_material_barcode');#条形码 ExBOY
        $omeMdl = app::get('ome')->model('orders');
        $omeExtOrdLib = kernel::single('ome_extint_order');
        $pro_id = $_POST['product_id'];
        if(!$_POST['shop_id']){
            $shopId = $this->shopIds;
        }else{
            $shopId = $_POST['shop_id'];
        }

        if (is_array($pro_id))
        {
            //多选判断
            if($pro_id[0]=='_ALL_')
            {
                $shopIds=implode("','", $shopId);
                $AXCustomer=$_GET['AXCustomer'];
                $sqlcustomer="SELECT shop_id from sdb_ome_shop where shop_id in('$shopIds') and AXCustomer='$AXCustomer' order by AXCustomer";
                $customer_data = kernel::database()->select($sqlcustomer);
                foreach ($customer_data as $key => $value) 
                {
                    $cdshopIds[]=$value['shop_id'];
                } 
                $filter['shop_id']=$cdshopIds;
                //获取操作员管辖仓库
                if ($_GET['btype'] == 2) {
                    //导出的时候走这里取第三方仓
                    $ownerBranch = array();
                    $ownerBranch = kernel::single('ome_branch_type')->getOtherBranchLists();
                } else {
                    //所有自建仓
                    $ownerBranch = array();
                    $ownerBranch = kernel::single('ome_branch_type')->getOwnBranchIds();
                }

                # 获取操作员管辖仓库
                $is_super = kernel::single('desktop_user')->is_super();
                if (!$is_super) {
                    $oBranch = app::get('ome')->model('branch');
                    $branch_ids = $oBranch->getBranchByUser(true);
                    if ($branch_ids) {
                        $filter['ext_branch_id'] = $_POST['branch_id'] ? $_POST['branch_id'] : $branch_ids;

                        $filter['ext_branch_id'] = array_intersect($filter['ext_branch_id'], $ownerBranch);
                    } else {
                        $filter['ext_branch_id'] = 'false';
                    }
                } else {
                    $filter['ext_branch_id'] = $ownerBranch;
                }

            }else{
                $filter['delivery_id'] = $pro_id;
            }
        }
        if (isset($_POST['filter']['advance']) && $_POST['filter']['advance'])
        {
            $arr_filters    = explode(',', $_POST['filter']['advance']);
            foreach ($arr_filters as $obj_filter)
            {
                $arr    = explode('=', $obj_filter);
                $filter[$arr[0]]    = $arr[1];
            }
            if($filter['_create_time_search'] == 'than' || $filter['_create_time_search'] == 'lthan')
            {
                $filter['_DTYPE_TIME'][0] = $filter['_DTYPE_TIME[]'];
                $filter['_DTIME_']['H']['create_time'] = $filter['_DTIME_[H][create_time]'];
                $filter['_DTIME_']['M']['create_time'] = $filter['_DTIME_[M][create_time]'];
            }
            if($filter['_create_time_search'] == 'between')
            {
                $filter['_DTYPE_TIME'][0] = $filter['_DTYPE_TIME[]'];
                $filter['_DTIME_']['H']['create_time_from'] = $filter['_DTIME_[H][create_time_from]'];
                $filter['_DTIME_']['M']['create_time_from'] = $filter['_DTIME_[M][create_time_from]'];
                $filter['_DTIME_']['H']['create_time_to'] = $filter['_DTIME_[H][create_time_to]'];
                $filter['_DTIME_']['M']['create_time_to'] = $filter['_DTIME_[M][create_time_to]'];
            }
        }
        $filter['print_status'] = '0';
        $filter['status'] = '0';
        $filter['is_ship'] = 'false';
        $filter['is_form'] = 'false';
        $filter['deliverytype'] = array('pickup','delivery','auto','batch','single');
        $delivery_model = $this->app->model('delivery');
        $Oshop = app::get('ome')->model('shop');
        $delivery= $delivery_model->getList('delivery_bn,delivery_id,shop_id,outer_delivery_bn,bnsContent',$filter);

        if (!empty($delivery))
        {
            foreach ($delivery as $k => $item)
            {

                $cnts = unserialize($item['bnsContent']);
                $order_bn = $omeExtOrdLib->getOrderBns($item['outer_delivery_bn']);
                $pause= $omeMdl->getList('pause',array('order_bn' => $order_bn));
                if ($pause[0]['pause'] == 'false') {
                    if($shopId){
                        if(in_array($item['shop_id'],$shopId)){
                            $item['product_id']=$item['delivery_id'];
                            $item['delivery_bn']=$item['delivery_bn'];
                            $item['shop_id']=$item['shop_id'];
                            $shop = $Oshop->dump(array('shop_id' => $item['shop_id']),'name');
                            $item['shop_name']=$shop['name'] ;
                            $item['bnsContent']=implode(',',$cnts);
                            $rows[]= $item;
                        }
                    }else{
                            $item['product_id']=$item['delivery_id'];
                            $item['delivery_bn']=$item['delivery_bn'];
                            $item['shop_id']=$item['shop_id'];
                            $shop = $Oshop->dump(array('shop_id' => $item['shop_id']),'name');
                            $item['shop_name']=$shop['name'] ;
                            $item['bnsContent']=implode(',',$cnts);
                            $rows[]= $item;
                    }
                }

            }
        }
        echo "window.autocompleter_json=".json_encode($rows);
    }


    //保存备货通知单
    function doSave()
    {
        $this->begin();
        $stockapp_model = $this->app->model('stockapp');
        $orderform_model = $this->app->model('orderform');
        $stockappDeil_model = $this->app->model('stockapp_delis');
        $orders_obj = $this->app->model('delivery');
        $deliveryBillLib = kernel::single('wms_delivery_bill');
        $Oshop = app::get('ome')->model('shop');
        $delivery_ID = $_POST['delivery_ID'];
        $name = $_POST['purchase_name'];
        $memo = $_POST['memo'];
        $id=$_POST['id'];
        $operator = $_POST['operator'];
        $orderformDeil_model = $this->app->model('orderform_delis');
        $orders_obj = $this->app->model('delivery');
        $deliveryBillLib = kernel::single('wms_delivery_bill');
        foreach ($delivery_ID as $k => $v) 
        {
            $orderform_id = $orderformDeil_model ->getList('id,delivery_bn',array('delivery_id'=>$v));
            if ($orderform_id) 
            {
                $this->end(false, $orderform_id[0]['delivery_bn'].'已生成备货通知单');
            }
            $stockapp_id = $stockappDeil_model ->getList('id,delivery_bn',array('delivery_id'=>$v));
            if ($stockapp_id) {
                $this->end(false, $stockapp_id[0]['delivery_bn'].'已生成备货申请单');
            }
            //add by ldward 2018-09-14 订单发货数量超出，需备货申请时校验
            $rows=$orderform_model->validateQty($v);
            if(!empty($rows))
            {              
                $orders = $orders_obj->getList('delivery_bn,delivery_id,shop_id', array('delivery_id'=>$v,'deliverytype'=> array('pickup','delivery','auto','batch','single')), 0, 1); 
                $this->end(false, '订单号：'.$rows[0]['order_bn'].'发货单号：'.$orders[0]['delivery_bn'].'发货数量异常，请联系管理员！');
            }
        }

        if(empty($id))
        {
            $stockapp_id = $stockapp_model ->getList('id',array('stockapp_bn'=>$_POST['purchase_name']));
            if ($stockapp_id) 
            {
                $this->end(false, '存在多人同时操作，请稍后重试');
            }
            $sqlcustomer="SELECT customername from sdb_ome_shop where AXCustomer='".$_POST['AXCustomer']."' order by AXCustomer";
            $customer_data = kernel::database()->select($sqlcustomer);

            $arr_order='';

            $arr_order['stockapp']['AXCustomer']=$_POST['AXCustomer'];
            $arr_order['stockapp']['customername']=$customer_data[0]['customername'];
            $arr_order['stockapp']['stockapp_bn']=$this->autocreatebn();//$_POST['purchase_name'];
            $arr_order['stockapp']['remark']=$_POST['memo'];
            $arr_order['stockapp']['operator']=$_POST['operator'];
            $arr_order['stockapp']['create_time']=time();
            $res = $stockapp_model->insert($arr_order['stockapp']);

            
        }else
        {
            $res=$id;
            $params=array(
                'id'=>$res,
                'remark'=>$_POST['memo'],
            );
            $stockapp_model->save($params);
            //删除明细
            kernel::database()->exec("update sdb_wms_delivery set is_form = 'false',deliverynote_time='".time()."' where  delivery_id in(select delivery_id from sdb_wms_stockapp_delis where stockapp_id in(".$id."));");
            kernel::database()->exec("delete from sdb_wms_stockapp_delis  where  stockapp_id in(" . $id . ");");
        } 

        foreach ($delivery_ID as $key => $value)
        {
            $orders = $orders_obj->getList('delivery_bn,delivery_id,shop_id,bnsContent', array('delivery_id'=>$value,'deliverytype'=> array('pickup','delivery','auto','batch','single')), 0, 1);

            $arr_line='';
            $arr_line['stockapp']['delivery_bn']=$orders[0]['delivery_bn'];
            $arr_line['stockapp']['bnsContent']=$orders[0]['bnsContent'];
            $arr_line['stockapp']['shop_id']=$orders[0]['shop_id'];
            $shop = $Oshop->dump(array('shop_id' => $orders[0]['shop_id']),'name');
            $arr_line['stockapp']['shop_name']=$shop['name'];
            $arr_line['stockapp']['delivery_id']=$value;
            $arr_line['stockapp']['stockapp_id']=$res;
            $stockappDeil_model->insert($arr_line['stockapp']);

            $orders_obj->db->exec("update sdb_wms_delivery set is_form = 'true',deliverynote_time='".time()."' where  delivery_id in(".$value.")");
        }
        $this->end(true, '已完成');
    }
    //提交动作
    function doConfirm()
    {
        $max_input_vars = ini_get('max_input_vars');
        ini_set('memory_limit','512M');
        set_time_limit(600);
        $this->begin("index.php?app=wms&ctl=admin_stockapp&act=index");
        $this->_request    = kernel::single('base_component_request');
        $order_info        = $this->_request->get_post();

        $stockapp_model = $this->app->model('stockapp');
        $stockapp_delis_model = $this->app->model('stockapp_delis');
        $orders_obj = $this->app->model('delivery');

        if ($order_info['isSelectedAll']=='_ALL_') {
            $orders = $stockapp_model->getList('*', array(), 0, -1);
            foreach ($orders as $key_o => $order) 
            {
                $value = $order['id']; 
                //确认后自动生成开立的审批单
                kernel::database()->exec("update sdb_wms_stockapp set status = 'confirmed',last_modify='".time()."' where  id in(".$value.");") ;
            }
        }else{
            $input = file_get_contents("php://input");
            $input_array = explode('&', $input);
            $number = count($input_array);
            if ($number > $max_input_vars) {
                $msg =  "多选不能超过系统表单最大提交数【".$max_input_vars."】条";
                $this->end(false, $msg);
            }
            foreach ($order_info['id'] as $key_o => $value)
            {
                kernel::database()->exec("update sdb_wms_stockapp set status = 'confirmed',last_modify='".time()."' where  id in(".$value.");");
            }
        }      //
        $this->end(true, '已完成');
    }
    function doPrint()
    {
        $operator = kernel::single('desktop_user')->get_name();
        $this->pagedata['operator'] = $operator;
        $this->pagedata['cur_date'] = date('YmdHis',time()).rand(1000,9999);
        $this->_request    = kernel::single('base_component_request');
        $print_post        = $this->_request->get_post();

        $orders_obj = $this->app->model('stockapp');
        $data = $orders_obj->getPrintInfo($print_post['id']);
        $this->pagedata['data'] = $data;
//        print_r($data);
        $this->singlepage('admin/stockapp/stockapp_print.html');
    }
    //取消确认动作
    function doundoConfirm()
    {
        $max_input_vars = ini_get('max_input_vars');
        ini_set('memory_limit','512M');
        set_time_limit(600);
        $this->begin("index.php?app=wms&ctl=admin_stockapp&act=index");
        $this->_request    = kernel::single('base_component_request');
        $order_info        = $this->_request->get_post();

        $db = kernel::database();
        $stockapp_model = $this->app->model('stockapp');
        $stockapp_delis_model = $this->app->model('stockapp_delis');
        $orders_obj = $this->app->model('delivery');
        $deliverynote_line_model = $this->app->model('deliverynote_line');

        if ($order_info['isSelectedAll']=='_ALL_') {
            $orders = $stockapp_model->getList('*', array(), 0, -1);
            foreach ($orders as $key_o => $order) {
                $value = $order['id'];
                if($order['status']=='confirmed')
                {                    
                    if($orders[0]['status']=='approved')
                    {
                        $this->end(false, '该备货申请提交审批，取消失败！');
                    }
                    $stockapp_model->db->exec("update sdb_wms_stockapp set status = 'unconfirmed',last_modify='".time()."' where  id in(".$value.")");
                 
                }else{

                    $this->end(false, '状态异常，取消失败！');           
                }
            }
        }else{
            $input = file_get_contents("php://input");
            $input_array = explode('&', $input);
            $number = count($input_array);
            if ($number > $max_input_vars) {
                $msg =  "多选不能超过系统表单最大提交数【".$max_input_vars."】条";
                $this->end(false, $msg);
            }
            foreach ($order_info['id'] as $key_o => $value)
            {
                $orders = $stockapp_model->getList('*', array('id'=>$value), 0, 1);

                if($orders[0]['status']=='confirmed')
                {
                    if($orders[0]['status']=='approved')
                    {
                        $this->end(false, '该备货申请提交审批，取消失败！');
                    }
                    $stockapp_model->db->exec("update sdb_wms_stockapp set status = 'unconfirmed',last_modify='".time()."' where  id in(".$value.")");
                }else{

                    $this->end(false, '状态异常，取消失败！');           
                }
            }
        }
        $this->end(true, '已完成');
    }
    //确认动作
    function doDelete()
    {
        $max_input_vars = ini_get('max_input_vars');
        ini_set('memory_limit','512M');
        set_time_limit(600);
        $this->begin("index.php?app=wms&ctl=admin_stockapp&act=index");
        $this->_request    = kernel::single('base_component_request');
        $order_info        = $this->_request->get_post();

        $db = kernel::database();
        $transaction =  $db->beginTransaction();
        $stockapp_model = $this->app->model('stockapp');

        if ($order_info['isSelectedAll']=='_ALL_') 
        {
            foreach ($orders as $key => $order) {
                $value = $order['id'];
                $orders = $stockapp_model->getList('*', array('id' => $value), 0, 1);
                if ($order['status'] == 'unconfirmed') 
                {

                    $db->exec("update sdb_wms_delivery set is_form = 'false',deliverynote_time='".time()."' where  delivery_id in(select delivery_id from sdb_wms_stockapp_delis where stockapp_id in(".$value."));");
                    $db->exec("delete from sdb_wms_stockapp_delis  where  stockapp_id in(" . $value . ");");
                    $db->exec("delete from sdb_wms_stockapp  where  id in(" . $value . ");");
                } else {
                    $this->end(false, '备货单号【' . $order['stockapp_bn'] . '】状态已提交，删除失败！');
                }
            }
        } else {
            $input = file_get_contents("php://input");
            $input_array = explode('&', $input);
            $number = count($input_array);
            if ($number > $max_input_vars) {
                $msg =  "多选不能超过系统表单最大提交数【".$max_input_vars."】条";
                $this->end(false, $msg);
            }
            foreach ($order_info['id'] as $key_o => $value) 
            {
                $orders = $stockapp_model->getList('*', array('id' => $value), 0, 1);

                if ($orders[0]['status'] == 'unconfirmed') 
                {

                    $db->exec("update sdb_wms_delivery set is_form = 'false',deliverynote_time='".time()."' where  delivery_id in(select delivery_id from sdb_wms_stockapp_delis where stockapp_id in(".$value."));");
                    $db->exec("delete from sdb_wms_stockapp_delis  where  stockapp_id in(" . $value . ");");
                    $db->exec("delete from sdb_wms_stockapp  where  id in(" . $value . ");");
                } else {
                    $this->end(false, '备货单号【' . $orders[0]['stockapp_bn'] . '】状态已提交，删除失败！');
                }
            }
        }
        $this->end(true, '已完成');
    }
    function editstockapp()
    {
        $stockapp_model = $this->app->model('stockapp');
        $stockapp_datas = $stockapp_model->getList('*', array('id'=>$_GET['id']), 0, -1);

        //1. 查询客户
        $sqlcustomer="SELECT distinct AXCustomer,customername 
            from sdb_ome_shop f where f.AXCustomer='".$stockapp_datas[0]['AXCustomer']."' ";
            
        $customer_data = kernel::database()->select($sqlcustomer);
        foreach ($customer_data as $key => $value) 
        {
            $customers[]= array('AXCustomer' => $value['AXCustomer'],'customername'=>'【'.$value['AXCustomer'].'】'.$value['customername']);   
        }
        $this->pagedata['stockapp'] = $stockapp_datas[0];
        $this->pagedata['stockapp']['customers'] = $customers;
        $this->singlepage('admin/stockapp/stockapp_edit.html');
    }

    function getEditProducts($po_id){
        if ($po_id == ''){
            $po_id = $_POST['p[0]'];
        }
        $piObj = $this->app->model('stockapp_delis');
        $stockapp_delis = $piObj->getList('*',array('stockapp_id'=>$po_id),0,-1);
        foreach ($stockapp_delis as $key => $value) {
            $row['product_id']=$value['delivery_id'];
            $row['delivery_id']=$value['delivery_id'];
            $row['delivery_bn']=$value['delivery_bn'];
            $row['shop_id']=$value['shop_id'];
            $row['shop_name']=$value['shop_name'];
            $cnts = unserialize($value['bnsContent']);
            $row['bnsContent']=implode(',',$cnts);
            $items[]=$row;
        }
        echo json_encode($items);
    }

    //保存备货通知单
    function createpickinglist($stockapp_ID)
    {
        $pickinglist_model = $this->app->model('pickinglist');
        $stockapp_model = $this->app->model('stockapp');
        $pickinglistDeil_model = $this->app->model('pickinglist_line');
        
        $arr_order='';
        $arr_order['pickinglist']['pickinglist_bn']=$this->autopicklistcreatebn();//$_POST['purchase_name'];
        $arr_order['pickinglist']['remark']='自动生成备货单';
        $arr_order['pickinglist']['operator']= kernel::single('desktop_user')->get_name();
        $arr_order['pickinglist']['create_time']=time();  
        $res = $pickinglist_model->insert($arr_order['pickinglist']);
        $stockapp_data = $stockapp_model ->getList('id,stockapp_bn,create_time,operator',array('id'=>$stockapp_ID));
        $arr_line='';
        $arr_line['pickinglist']['stockapp_bn']=$stockapp_data[0]['stockapp_bn'];
        $arr_line['pickinglist']['stockapp_id']=$stockapp_ID;
        $arr_line['pickinglist']['pickinglist_id']=$res;
        $arr_line['pickinglist']['app_time']=$stockapp_data[0]['create_time'];
        $arr_line['pickinglist']['operator']=$stockapp_data[0]['operator'];
        $pickinglistDeil_model->insert($arr_line['pickinglist']);
        
    }
    function autopicklistcreatebn()
    {
        $bn='';
        $y = date("Y");
        $m = date("m");
        $d = date("d");
        $time_begin= mktime(0,0,0,$m,$d,$y);
        $time_end= mktime(23,59,59,$m,$d,$y);
        $delivery_model = $this->app->model('pickinglist');
        $filter['create_time|between'] = array($time_begin, $time_end);
        $delivery= $delivery_model->getList('pickinglist_bn',$filter,0,1,'create_time desc');
        if($delivery[0]['pickinglist_bn']=='')
        {
            $bn ='SP'.date('Ymd',time()).'01';
        }
        else
        {
            $bn=substr($delivery[0]['pickinglist_bn'] , 10 , 2);
            $num=intval($bn)+1;
            if($num<=9)
            {
                $bn ='SP'.date('Ymd',time()).'0'.$num;
            }else
            {
                $bn ='SP'.date('Ymd',time()).$num;
            }
        }
        return $bn;
    }
}
